library SelectENCommand initializer init requires LegacySystem, PlayerSelectsHeroCommand, LegacyHumanAbilities


	globals
		private trigger                 udg_EngyCommArray          = null
		private trigger                 udg_EngyDetonateArray      = null
		private trigger                 udg_EngyArrayDestroyed     = null
		unit                    udg_CommArray              = null
	endglobals
	
	private function ForceIsEmpty_Enum takes nothing returns nothing
		set bj_isUnitGroupEmptyResult = false
	endfunction

	private function ForceIsEmpty takes force whichForce returns boolean
		set bj_isUnitGroupEmptyResult = true
		call ForForce(whichForce, function ForceIsEmpty_Enum)
		return bj_isUnitGroupEmptyResult
	endfunction

	private function SetForceSharedControlNoVision takes force sourceForce, force targetForce returns nothing
		local integer sourceIndex = 0
		local integer targetIndex
		loop
			if (sourceForce==bj_FORCE_ALL_PLAYERS or IsPlayerInForce(Player(sourceIndex), sourceForce)) then
				set targetIndex = 0
				loop
					if (targetForce==bj_FORCE_ALL_PLAYERS or IsPlayerInForce(Player(targetIndex), targetForce)) then
						call SetPlayerAlliance(Player(sourceIndex), Player(targetIndex), ALLIANCE_SHARED_VISION, false)
					endif

					set targetIndex = targetIndex + 1
					exitwhen targetIndex == bj_MAX_PLAYER_SLOTS
				endloop
			endif
			set sourceIndex = sourceIndex + 1
			exitwhen sourceIndex == bj_MAX_PLAYER_SLOTS
		endloop
	endfunction


	private function CommArrayDestroyed takes nothing returns nothing
		local integer A = 1
		local integer B = 1
		local player p = GetOwningPlayer(GetTriggerUnit())
		if GetTriggerUnit()!=udg_CommArray then
			return
		endif
		call DisableTrigger(udg_EngyArrayDestroyed)
		call PolledWait(2.)
		call ALICEText(5.0, "|cffcd950cALICE|r : Communications link broken." )
		if GetWidgetLife(udg_Hero[GetConvertedPlayerId(p)])>0.405 then
			call ModifyHeroStat( bj_HEROSTAT_INT, udg_Hero[GetConvertedPlayerId(p)], bj_MODIFYMETHOD_ADD, 8 )
		endif
		set udg_XPFactor = udg_XPFactor + 0.03
		if udg_Mode==1 or udg_Mode==2 then
			call SetForceAllianceStateBJ(udg_EchoCompany,udg_EchoCompany,bj_ALLIANCE_ALLIED)
			call SetForceSharedControlNoVision(udg_EchoCasualties,udg_EchoCompany)
		endif
		call EnableTrigger(udg_EngyCommArray)
	endfunction

	private function DetonateCommArray takes nothing returns nothing
		if GetSpellAbilityId()=='A048' then
			call PolledWait(2.)
			call KillUnit(GetTriggerUnit())
		endif
	endfunction



	private function EstablishCommArrayConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A01P'
	endfunction

	private function EstablishCommArray takes nothing returns nothing
		local group g = GetUnitsOfTypeIdAll('o001')
		local unit u  = FirstOfGroup(g)
		local real x = GetUnitX(u)
		local real y = GetUnitY(u)
		local player p = GetOwningPlayer(GetTriggerUnit())
		call DestroyGroup(g)
		call RemoveUnit(u)
		set u = null
		set g = null
		if RectContainsCoords(udg_PlayerSpawnBoundary,x,y) then
			call DisableTrigger(udg_EngyCommArray)
		else
			call ALICEText(5.0, "Insufficient Broadcast Strength. Establish Further Inland." )
			return
		endif
		call PolledWait(0.1)
		set udg_CommArray = CreateUnit(p,'o001',x,y,0.0)
		call EnableTrigger(udg_EngyArrayDestroyed)
		call DisplayTextToPlayer(GetLocalPlayer(),0.0,0.0, "|cffcd950cALICE|r : Communications Array activated. Broadcasting squad signatures.." )
		call PolledWait(2.25)
		if GetWidgetLife(udg_Hero[GetConvertedPlayerId(p)])>0.405 then
			call ModifyHeroStat( bj_HEROSTAT_INT, udg_Hero[GetConvertedPlayerId(p)], bj_MODIFYMETHOD_SUB, 8 )
		endif
		call StartSound(gg_snd_SatelliteImaging)
		set udg_XPFactor = udg_XPFactor-0.03
		if udg_Mode==1 or udg_Mode==2 then
			call SetForceAllianceStateBJ(udg_EchoCompany,udg_EchoCompany,bj_ALLIANCE_ALLIED_VISION)
		endif
	endfunction

	private function EngineerInit takes unit u returns nothing
		local trigger t = CreateTrigger() 

		set udg_EngyCommArray = CreateTrigger()
		call TriggerRegisterUnitEvent( udg_EngyCommArray, u, EVENT_UNIT_SPELL_FINISH )
		call TriggerAddCondition( udg_EngyCommArray, Condition( function EstablishCommArrayConditions ) )
		call TriggerAddAction( udg_EngyCommArray, function EstablishCommArray )

		set udg_EngyDetonateArray = CreateTrigger()
		call TriggerRegisterPlayerUnitEventSimple( udg_EngyDetonateArray, GetOwningPlayer(u), EVENT_PLAYER_UNIT_SPELL_EFFECT )
		call TriggerAddAction( udg_EngyDetonateArray, function DetonateCommArray )

		set udg_EngyArrayDestroyed = CreateTrigger()
		call DisableTrigger( udg_EngyArrayDestroyed )
		call TriggerRegisterPlayerUnitEvent(udg_EngyArrayDestroyed, GetOwningPlayer(u), EVENT_PLAYER_UNIT_DEATH, null)
		call TriggerAddAction( udg_EngyArrayDestroyed, function CommArrayDestroyed )
		set t = null
	endfunction

	//==============================================================
	// Actuall trigger here
	//=============================================================


	private function Trig_Player_Selects_ENG_Conditions takes nothing returns boolean
		if ( not ( GetUnitTypeId(GetTriggerUnit()) == 'H007' ) ) then
			return false
		endif
		return true
	endfunction


	private function Trig_Player_Selects_ENG_Actions takes nothing returns nothing

		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer id = GetConvertedPlayerId(p)

		
		set udg_EngyCommArray = CreateTrigger()
		set udg_EngyDetonateArray      = CreateTrigger()
		set udg_EngyArrayDestroyed     = CreateTrigger()
		
		call putUnit(p,"hero", u)
		call putUnit(null,"ENGUnit", u)   


		call setPlayerDataInteger(p,"pistol_ammo",8)
		call setPlayerDataInteger(p,"pistol_type",0)
		call setPlayerDataBoolean(p,"pistol_reload",false)

		call SetUnitUserData(u,18)
			
		call putBoolean(null, "ENGPresent", true)
		set udg_PlayerIcon[id] = "ReplaceableTextures\\CommandButtons\\BTN_Unit_Engineer.blp"
		set udg_HeroAvailable[3] = 0
		set udg_KillFactor[id] = 1.8
		   
		call UnitAddItemById(u,'I014') //Field Lights
		call UnitAddItemById(u,'I00U') //Det cords

		call AddPrimaryAmmo(u, 15, 18)
		call RegisterAmmoAutoReloadListener(u)
		call RegisterAmmoEmptyListener(u)
		
		call InitHero(u)
		call EngineerInit(u)

		if GetI("player mos", id) == 4 then
			call ModifyHeroSkillPoints( u, bj_MODIFYMETHOD_ADD, 1 )
		endif
		
		set u = null
		set p = null
		
		if( udg_HeroAvailable[3] <= 0 ) then
			call DestroyTrigger(GetTriggeringTrigger())
		endif

	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterEnterRectSimple( t, GetPlayableMapRect() )
		call TriggerAddCondition( t, Condition( function Trig_Player_Selects_ENG_Conditions ) )
		call TriggerAddAction( t, function Trig_Player_Selects_ENG_Actions )
	endfunction


endlibrary